/**
 * =======================================================================<br>
 * 版权： 福富软件 版权所有 (c) 2007 - 2008<br>
 * 文件包： com.xhui.crm.common.exception 所含类： ExceptionHandler 编写人员：陈军
 * 创建日期：2007-03-27 功能说明: 使用Throw通知类型来实现Advice 更新记录： 日期 作者 内容<br>
 * =======================================================================<br>
 * 2007-03-29 陈军 创建新文件，并实现afterThrowing方法
 * =======================================================================<br>
 */
package com.xhui.vimarket.common.exception;

import java.io.Writer;
import java.lang.reflect.Method;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.aop.ThrowsAdvice;

import com.xhui.vimarket.common.utils.date.DateUtil;

/*
 * 创建 异常捕捉并输出信息
 */
public class ExceptionHandler implements ThrowsAdvice {
    
    private final Logger logger = Logger.getLogger(this.getClass().getName());
    

    public void afterThrowing(final Method method, final Object[] args,
        final Object target, final Throwable subclass) throws Throwable {
        
        try {
            
            this.logger
                .log(Level.ERROR,
                    "---------------------当前方法抛出异常------------------------------------------------");
            this.logger.log(Level.ERROR, "执行时间: " + DateUtil.getNowCustomDate()
                + " " + DateUtil.getNowCustomTime());
            
            this.logger.log(Level.ERROR, "类名: " + method.getDeclaringClass()
                + "  方法名:" + method.getName());
            this.logger.log(Level.ERROR, " 执行时有异常抛出....");
            this.logger.log(Level.ERROR, " 异常信息是:" + subclass);
            
            final StackTraceElement stackTraceElement = subclass
                .getStackTrace()[0];// 得到异常栈的首个元素
            
            this.logger.log(Level.ERROR, "文件名:"
                + stackTraceElement.getFileName()); // 打印文件名
            this.logger.log(Level.ERROR, "出错方法:"
                + stackTraceElement.getMethodName());// 打印出错方法
            this.logger.log(Level.ERROR, "出错行号:"
                + stackTraceElement.getLineNumber());// 打印出错行号
            
            this.logger
                .log(
                    Level.ERROR,
                    "-----------------------------------------------------------------------------------");
            
        } catch (final Exception ex) {
            this.logger.log(Level.ERROR, "输出异常日志时出错!");
        }
        try {
            this.logger.log(Level.DEBUG, " 输入信息是:");
            if (args != null) {
                final Writer inputParam = new java.io.StringWriter();
                org.exolab.castor.xml.Marshaller.marshal(args, inputParam);
                this.logger.log(Level.DEBUG, inputParam);
            }
            // logger.log(Level.DEBUG, " 输出信息是:");
            // if (target != null) {
            // Writer outputParam = new java.io.StringWriter();
            // org.exolab.castor.xml.Marshaller.marshal(target, outputParam);
            // logger.log(Level.DEBUG, outputParam);
            // }
            
        } catch (final Exception ex) {
            this.logger.log(Level.DEBUG, "输入转换成XML时发生异常!");
        }
        
    }
    
}
